//
//  RentHouseCategoryVC.m
//  CenturyCity
//
//  Created by beisheng03 on 2018/1/8.
//  Copyright © 2018年 beisheng. All rights reserved.
//

#import "RentHouseCategoryVC.h"
#import "RentHouseNavView.h"
#import "RenHouseTopImageCell.h"
#import "RenHouseHomeListCell.h"
#import <DOPDropDownMenu.h>
#import "RentHouseDetailVC.h"
#import "HouseListParameter.h"
#import "HousRentAdvsModel.h"
#import "HouseParameterModel.h"
#import "HouseParameSub.h"
#import "ProvinceModel.h"
#import "CitysModel.h"
#import "DistrictsModel.h"
#import "RentHouseNothingCell.h"
@interface RentHouseCategoryVC ()<DOPDropDownMenuDataSource,DOPDropDownMenuDelegate>


@property (nonatomic, assign) NSInteger curpage;

@property (nonatomic, assign) NSString *pageCount;

@property (nonatomic, assign) NSString *totalCount;

@property (nonatomic, strong) NSMutableArray<HouseParameterModel *> *dataModels;


@property (nonatomic, weak) DOPDropDownMenu *menu;

/** <#note#> */
@property (nonatomic, strong) HouseListParameter *dataParameters;


@end

@implementation RentHouseCategoryVC

- (HouseListParameter *)dataParameters{
    
    if (!_dataParameters) {
        
        _dataParameters = [[HouseListParameter alloc]init];
        
    }
    
    return _dataParameters;
}



- (DOPDropDownMenu *)menu{
    if (!_menu) {
        
        
        CGFloat dropY = IPhoneX ? 88 : 64;
        
        //        NSLog(@"%f",dropY);
        
        // 添加下拉菜单
        DOPDropDownMenu *menu = [[DOPDropDownMenu alloc] initWithOrigin:CGPointMake(0, dropY) andHeight:45];
        menu.delegate = self;
        menu.dataSource = self;
        menu.isClickHaveItemValid = NO;
        [self.view addSubview:menu];
        self.menu = menu;
        
        self.menu.hidden = YES;
        
        [menu selectIndexPath:[DOPIndexPath indexPathWithCol:0 row:0 item:0]];
    }
    
    
    
    return _menu;
}








- (void)popCurrentController{
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];
  
    
    NSString *temp = nil;
    if ([self.type isEqualToString:@"0"]) {
        
        self.navigationItem.title = @"整租";
        
        temp = @"整租";
        
    } else if ([self.type isEqualToString:@"1"]) {
        
        self.navigationItem.title = @"短租";
        
        temp = @"短租";
    }else{
        
        self.navigationItem.title = @"合租";
        
        temp = @"合租";
    }
    
    
        for (HouseParameSub *sub1 in myViewModel.parameterModel.rentingStyle) {
            if ([sub1.name isEqualToString:temp]) {
                self.dataParameters.rentingStyleId = sub1.class_id;
            }
        }
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.groupTableView.tableFooterView = [[UIView alloc]init];
    self.groupTableView.separatorInset = UIEdgeInsetsZero;
    self.groupTableView.backgroundColor = [UIColor whiteColor];
    
    [self.groupTableView registerClass:[BaseTableViewCell class] forCellReuseIdentifier:[BaseTableViewCell identifierStr]];
    
    [self.groupTableView registerNib:[UINib nibWithNibName:NSStringFromClass([RenHouseHomeListCell class]) bundle:nil] forCellReuseIdentifier:[RenHouseHomeListCell identifierStr]];
    
    [self.groupTableView registerNib:[UINib nibWithNibName:NSStringFromClass([RentHouseNothingCell class]) bundle:nil] forCellReuseIdentifier:[RentHouseNothingCell identifierStr]];

    
    
    //    CGFloat navH = IPhoneX ? 44 : 20;
    CGFloat tabbarH = IPhoneX ? 132 : 108;
    
    if (@available(iOS 11.0, *)) {
        //        contentInsetAdjustmentBehavior属性也是用来取代automaticallyAdjustsScrollViewInsets属性的，推荐使用这种方式。
        self.groupTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        
        self.groupTableView.contentInset = UIEdgeInsetsMake(tabbarH, 0, 0, 0);
        
    }else{
        
        self.groupTableView.contentInset = UIEdgeInsetsMake(tabbarH, 0, 0, 0);
        
    }
    

    
    
    
    MJWeakSelf
    self.groupTableView.mj_header = [XMGRefreshHeader headerWithRefreshingBlock:^{
        
        [weakSelf loadData];
        
    }];
    
    [weakSelf.groupTableView.mj_header beginRefreshing];
    
    
//    self.groupTableView.mj_footer = [XMGRefreshFooter footerWithRefreshingBlock:^{
//        [weakSelf loadMoreData];
//    }];
    

    
    
}


/**
 * 房源列表接口
 * page                 当前第几页 （默认1）
 * pagesize             每页数据总条数（默认10）
 * languageId           语种id
 * provinceId           所在区域一级地区
 * cityId               所在区域二级地区
 * payClassId           付款方式id
 * rentingStyleId       租房方式id
 
 */

- (void)loadData{
    
    
    self.curpage = 1;
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"page"] = @"1";
    parameters[@"pagesize"] = @"20";
    
    if (self.dataParameters.languageId.length > 0) {
        parameters[@"languageId"] = self.dataParameters.languageId;
        
    }
    
    if (self.dataParameters.provinceId.length > 0) {
        parameters[@"provinceId"] = self.dataParameters.provinceId;
        
    }
    
    if (self.dataParameters.cityId.length > 0) {
        parameters[@"cityId"] = self.dataParameters.cityId;
        
    }
    
    if (self.dataParameters.payClassId.length > 0) {
        parameters[@"payClassId"] = self.dataParameters.payClassId;
        
    }
    
    if (self.dataParameters.rentingStyleId.length > 0) {
        parameters[@"rentingStyleId"] = self.dataParameters.rentingStyleId;
        
    }
    
    
    MJWeakSelf;
    [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:GET_METHOD URLPath:@"house/lists" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
            
            //            MYJSONSTR
            //            _model.lastString
            
            //           [[responseObject[@"data"][@"house"] firstObject]  propertyCode];
            
            weakSelf.pageCount = responseObject[@"data"][@"page_count"];
            
            weakSelf.totalCount = responseObject[@"data"][@"total_count"];
            
            
            
            //            [weakSelf.ADVSModels removeAllObjects];
            weakSelf.dataModels = [HouseParameterModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"house"]];
            
            if (myViewModel.parameterModel != nil) {
                for (HouseParameterModel *model in weakSelf.dataModels) {
                    
                    model.language = myViewModel.parameterModel.language;
                    model.huxing = myViewModel.parameterModel.huxing;
                    model.fukuan = myViewModel.parameterModel.fukuan;
                    model.rentingStyle = myViewModel.parameterModel.rentingStyle;
                    model.decorateStyle = myViewModel.parameterModel.decorateStyle;
                    model.houseStyle = myViewModel.parameterModel.houseStyle;
                    model.position = myViewModel.parameterModel.position;
                    
                    [weakSelf setupValues:model];
                }
            }
            
            
            [weakSelf.groupTableView.mj_footer resetNoMoreData];
            
            //            weakSelf.menu.hidden = NO;
            
            
            if ([weakSelf.totalCount isEqualToString:@"0"]) {
                
                weakSelf.groupTableView.mj_footer.hidden = YES;
                
                weakSelf.groupTableView.mj_footer.height = 0;
                
                weakSelf.groupTableView.mj_footer = nil;
                
                [weakSelf.groupTableView.mj_footer endRefreshingWithNoMoreData];
                
            } else {
                
                
                self.menu.hidden = NO;
                
                weakSelf.groupTableView.mj_footer = [XMGRefreshFooter footerWithRefreshingBlock:^{
                    
                    [weakSelf loadMoreData];
                    
                }];
                
                [weakSelf.groupTableView.mj_footer resetNoMoreData];
                
                
                if ([weakSelf.pageCount isEqualToString:@"1"]) {
                    
                    [weakSelf.groupTableView.mj_footer endRefreshingWithNoMoreData];
                    
                }
                
     
            }
            
            
     
            
            
            
            
            
          
                
                
                
                [weakSelf.groupTableView.mj_header endRefreshing];
                
                [weakSelf.groupTableView reloadData];
          
            
            
            
            
        } else {
            
            [weakSelf.groupTableView.mj_header endRefreshing];
            
            [SVProgressHUD showInfoWithStatus:error.domain];
            
        }
    }];
    
    
    
    
    
    
    
    
    
    
}






- (void)loadMoreData{
    
    
    
    self.curpage ++;
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"pagesize"] = @"20";
    parameters[@"page"] = [NSString stringWithFormat:@"%ld",(long)self.curpage];
    
    if (self.dataParameters.languageId.length > 0) {
        parameters[@"languageId"] = self.dataParameters.languageId;
        
    }
    
    if (self.dataParameters.provinceId.length > 0) {
        parameters[@"provinceId"] = self.dataParameters.provinceId;
        
    }
    
    if (self.dataParameters.cityId.length > 0) {
        parameters[@"cityId"] = self.dataParameters.cityId;
        
    }
    
    if (self.dataParameters.payClassId.length > 0) {
        
        parameters[@"payClassId"] = self.dataParameters.payClassId;
        
    }
    
    
    if (self.dataParameters.rentingStyleId.length > 0) {
        parameters[@"rentingStyleId"] = self.dataParameters.rentingStyleId;
        
    }
    
    MJWeakSelf;
    [[NetworkTools shareNetworkTools] sendRequestWithHttpMethod:GET_METHOD URLPath:@"house/lists" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
            
            //            MYJSONSTR;
            
            //            weakSelf.ADVSModels = [HousRentAdvsModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"advs"]];
            
            
            
            NSMutableArray<HouseParameterModel *> *temp = [HouseParameterModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"house"]];
            
            
            
            if (myViewModel.parameterModel != nil) {
                
                for (HouseParameterModel *model in temp) {
                    
                    model.language = myViewModel.parameterModel.language;
                    model.huxing = myViewModel.parameterModel.huxing;
                    model.fukuan = myViewModel.parameterModel.fukuan;
                    model.rentingStyle = myViewModel.parameterModel.rentingStyle;
                    model.decorateStyle = myViewModel.parameterModel.decorateStyle;
                    model.houseStyle = myViewModel.parameterModel.houseStyle;
                    model.position = myViewModel.parameterModel.position;
                    
                    [weakSelf setupValues:model];
                    
                }
            }
            
            
            [weakSelf.dataModels addObjectsFromArray:temp];
            
            
            
            
            
            
            
            if (weakSelf.pageCount.integerValue == self.curpage) {
                
                [weakSelf.groupTableView.mj_footer endRefreshingWithNoMoreData];
                
            } else {
                
                [weakSelf.groupTableView.mj_footer endRefreshing];
                
                
            }
            
            
            [weakSelf.groupTableView reloadData];
            
        }else{
            
            [weakSelf.groupTableView.mj_footer endRefreshing];
            
            [SVProgressHUD showInfoWithStatus:error.domain];
            
        }
        
    }];
    
    
    
    
    
}




- (void)setupValues:(HouseParameterModel *)model{
    
    
    
    for (HouseParameSub *sub1 in model.position) {
        if ([sub1.class_id isEqualToString:model.house_face]) {
            model.house_face_name = sub1.name;
        }
    }
    
    
    
    
//    for (HouseParameSub *sub2 in model.language) {
//        if ([sub2.class_id isEqualToString:model.house_language]) {
//            model.house_language_name = sub2.name;
//        }
//    }
    
    
    for (HouseParameSub *sub2 in model.decorateStyle) {
        if ([sub2.class_id isEqualToString:model.house_decorate]) {
            model.house_decorate_name = sub2.name;
        }
    }
    
    
    
    
    
    
    
    
    
}







//- (void)viewWillAppear:(BOOL)animated
//{
//    [super viewWillAppear:animated];
//    
////    [self.navigationController setNavigationBarHidden:YES animated:YES];
//    
//    
//}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    if (self.dataModels == nil) {
        return 0;
    } else {
        return 1;
    }
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    if (self.dataModels == nil) {
        return 0;
    } else {
        
        if (self.dataModels.count == 0) {
            
            return 1;
            
        } else {
            
            return self.dataModels.count;

        }
    }
    
    
    
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.dataModels.count == 0) {
         RentHouseNothingCell *cell = [tableView dequeueReusableCellWithIdentifier:[ RentHouseNothingCell identifierStr]];
        
        
        return cell;
        
    } else {
        RenHouseHomeListCell *cell = [tableView dequeueReusableCellWithIdentifier:[RenHouseHomeListCell identifierStr]];
        
        cell.model = self.dataModels[indexPath.row];
        
        return cell;
    }
  
    

    
   
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (self.dataModels.count == 0) {
        
        
        return  KScreenH - (IPhoneX ? 132 : 108);
        
    } else {
        
        return 124;
    }
    
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.00001;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    return 0.00001;
    
    
    
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return [[UIView alloc]init];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return [[UIView alloc]init];
    
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    RentHouseDetailVC *temp = [[RentHouseDetailVC alloc]init];
    
    temp.houseId = self.dataModels[indexPath.row].house_id;

    
    [self.navigationController pushViewController:temp animated:YES];
    
    
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}







#pragma mark - actions

- (NSInteger)numberOfColumnsInMenu:(DOPDropDownMenu *)menu
{
    return 3;
}

- (NSInteger)menu:(DOPDropDownMenu *)menu numberOfRowsInColumn:(NSInteger)column
{
    
    
    if (column == 0) {
        
        return myViewModel.parameterModel.language.count + 1;
        
        
    }else if (column == 1){
        
        return myViewModel.areas.count + 1;
        
    }else {
        return myViewModel.parameterModel.fukuan.count + 1;
    }
}

- (NSString *)menu:(DOPDropDownMenu *)menu titleForRowAtIndexPath:(DOPIndexPath *)indexPath
{
    if (indexPath.column == 0) {
        
        if (indexPath.row == 0) {
            return @"全部语种";
        } else {
            return myViewModel.parameterModel.language[indexPath.row - 1].name;
            
        }
        
        
        
        
        
        
    } else if (indexPath.column == 1){
        
        if (indexPath.row == 0) {
            return @"全部区域";
        } else {
            return myViewModel.areas[indexPath.row - 1].province_name;
        }
        
        
        
        
        
    } else {
        
        if (indexPath.row == 0) {
            return @"全部方式";
        } else {
            return myViewModel.parameterModel.fukuan[indexPath.row - 1].name;
            
        }
        
        
        
        
        
    }
}



- (NSInteger)menu:(DOPDropDownMenu *)menu numberOfItemsInRow:(NSInteger)row column:(NSInteger)column
{
    
    if (column == 1) {
        
        if (row == 0) {
            return 1;
        } else {
            return myViewModel.areas[row - 1].citys.count;
            
        }
        
        
        
        
        
        
        
    } else {
        
        return 0;
    }
    
    
}

- (NSString *)menu:(DOPDropDownMenu *)menu titleForItemsInRowAtIndexPath:(DOPIndexPath *)indexPath
{
    
    
    if (indexPath.column == 1) {
        
        if (indexPath.row == 0) {
            
            return @"全部区域";
            
        } else {
            
            return myViewModel.areas[indexPath.row - 1].citys[indexPath.item].city_name;
            
        }
        
        
        
        
    } else {
        
        return @"";
        
    }
    
    
    
}


- (void)menu:(DOPDropDownMenu *)menu didSelectRowAtIndexPath:(DOPIndexPath *)indexPath
{
    //    [self.view bringSubviewToFront:self.headerView];
    //    NSLog(@"点击了。。。。");
    
    //    return;
    if (indexPath.column == 0) {
        
        if (indexPath.row == 0) {
            
            self.dataParameters.languageId = nil;
            
        } else {
            
            HouseParameSub *sub =  myViewModel.parameterModel.language[indexPath.row - 1];
            
            self.dataParameters.languageId = sub.class_id;
        }
        
        
        
        
        
    }else if (indexPath.column == 1){
        
        if (indexPath.row == 0) {
            
            self.dataParameters.provinceId = nil;
            self.dataParameters.cityId = nil;
            
        } else {
            
            ProvinceModel *provice =  myViewModel.areas[indexPath.row - 1];
            
            self.dataParameters.provinceId = provice.province_id;
            
            CitysModel *cityMode = provice.citys[indexPath.item];
            
            self.dataParameters.cityId = cityMode.city_id;
            
        }
        
        
        
    }else {
        
        
        if (indexPath.row == 0) {
            
            self.dataParameters.payClassId = nil;
            
        } else {
            
            HouseParameSub *sub =  myViewModel.parameterModel.fukuan[indexPath.row - 1];
            
            self.dataParameters.payClassId = sub.class_id;
        }
        
        
        
        
    }
    
    
    [self loadData];
    
}






@end
